#include "GamePanel.h"

GamePanel::GamePanel(QWidget* parent)
    : QWidget(parent)
    , list_label(new QLabel("玩家列表", this))
    , player_list(new QListWidget(this))
    , widget_layout(new QVBoxLayout(this))
    , start_button(new QPushButton(START_TEXT, this))
{
    list_label->setAlignment(Qt::AlignCenter);

    widget_layout->addWidget(list_label);
    widget_layout->addWidget(player_list);
    widget_layout->addWidget(start_button);

    connect(
        start_button, SIGNAL(clicked()), this, SLOT(onStartButtonClicked()));

    disable();
}

void GamePanel::addPlayer(const QString& name)
{
    player_list->addItem(name);
}

void GamePanel::enable()
{
    player_list->setEnabled(true);
    start_button->setEnabled(true);
}

void GamePanel::disable(bool all)
{
    player_list->setEnabled(false);
    if (all) {
        start_button->setEnabled(false);
        start_button->setText(START_TEXT);
    }
}

void GamePanel::onStartButtonClicked()
{
    if (start_button->text() == START_TEXT) {
        start_button->setText(FINISH_TEXT);
        emit started();
    } else if (start_button->text() == FINISH_TEXT) {
        start_button->setText(START_TEXT);
        emit finished();
    }
}

void GamePanel::removePlayer(const QString& name)
{
    for (const auto& item : player_list->findItems(name, Qt::MatchExactly)) {
        player_list->takeItem(player_list->row(item));
    }
}
